<?php
namespace Home\Controller;
use Think\Controller;

class CompanyController extends Controller {
	
	public function __construct(){
		parent::__construct();
		
		if(empty($_SESSION['user']))
		{
			 $this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
			
		}
	}

	//添加公司信息
	public function info(){

		$cmp = M('company_info');
		if(IS_POST){
			$data=I('post.');
			$data['create_time']=date('Y-m-d H:i:s');
			if($data['id']){
				$addData = $cmp->save($data);
				if($addData!==false){
					$this->success('更新成功','info');
				}else{
					$this->error('服务器忙，稍等....');
				}
			}else{  //新增 无ID
				$addData = $cmp->add($data);
				if($addData){
					$this->success('设置成功','info');
				}else{
					$this->error('服务器忙，稍等....');
				}
			}
			
			
			
		}
		// get 方式打开
		
		$getInfo = $cmp->find(1);
		$getInfo['brief'] = html_entity_decode($getInfo['brief']);  //html_entity_decode 该函数将 html 转化， 就不会显示html 代码
		$getInfo['enbrief'] = html_entity_decode($getInfo['enbrief']);
		$getInfo['contact'] = html_entity_decode($getInfo['contact']);
		$this->assign('getInfo',$getInfo);
		$this->display();
	}

	

	




	
	public function newsManage(){
		$new = M('news');

		if(IS_POST){

			$add_client = I('post.','','htmlspecialchars');
			
			$res = M('client_info')->add($add_client);

			if($res){
				$this->success('OK','lst');
			}else{
				$this->error('Fail');
			}

		}


		//新闻管理列表
		
		$get_news_list = $new->select();

		foreach($get_news_list as &$v){
			$v['body'] = html_entity_decode($v['body']); 
			$v['enbody'] = html_entity_decode($v['enbody']); 
		}

		$this->assign([
			'get_news_list' => $get_news_list,
		]);

		$this->display();
	}

	//新闻添加
	public function news_add(){
		$new = M('news');

		if(IS_POST){

			$data = I('post.','','htmlspecialchars');
			$data['creator'] = session('user');
			$data['createtime'] = date('Y-m-d H:i:s');
			
			$res = $new->add($data);

			if($res){
				$this->success('添加新闻成功','newsManage',1);
			}else{
				$this->error('添加新闻失败');
			}

		}

		$this->display();
	}


	//新闻编辑
	public function news_edit(){
		$new = M('news');
		$id = intval(I('get.id'));

		if(IS_POST){

			$data = I('post.','','htmlspecialchars');
			
			$res = $new->save($data);

			if($res!==false){
				$this->success('修改新闻成功',U('newsManage'),1);
			}else{
				$this->error('修改新闻失败');
			}

		}

		$get_news_info = $new->find($id);

		
		$get_news_info['body'] = html_entity_decode($get_news_info['body']); 
		$get_news_info['enbody'] = html_entity_decode($get_news_info['enbody']); 
		

		$this->assign([
			'get_news_info' => $get_news_info,
		]);

		$this->display();
	}

	//删除新闻
	public function news_del(){
		$id = intval(I('get.id'));

		$res = M('news')->delete($id);

		if($res!==false){
			$this->success('成功删除',U('newsManage'));
		}else{
			$this->error('删除新闻失败');
		}


	}


}